package org.keyuan.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.keyuan.dao.BookDao;
import org.keyuan.entity.Book;

public class BookDaoImpl extends DaoSupport<Book> implements BookDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> sellTop(Date beginDate) {
		
		//String sql="select * from Bookinfo bi where bi.bookid in(select bookTest.bookId from (select ot.productid bookId,count(*) cout from orderiteminfo ot where ot.order_id in(select oi.orderid from orderinfo oi where oi.createdate between to_date(?,'yyyy-MM-dd') and to_date(?,'yyyy-MM-dd')) group by ot.productid order by cout ) bookTest)";
		String sql="select * from Bookinfo bi where bi.bookid in(select bookTest.bookId from (select ot.productid bookId,count(*) cout from orderiteminfo ot where ot.order_id in(select oi.orderid from orderinfo oi where oi.createdate between ? and ?) group by ot.productid order by cout ) bookTest)";
		Query query=this.getSession().createSQLQuery(sql).addEntity(Book.class);
		Calendar date=Calendar.getInstance();
		date.set(2011, 5, 1);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		query.setParameter(0,simpleDateFormat.format(beginDate));
		query.setParameter(1,simpleDateFormat.format(date.getTime()));
		query.setFirstResult(0);
		query.setMaxResults(10);
		return query.list();
		
	}

}
